package com.smec.moblie.location;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.net.Uri;

/***
 * 经纬度计算相关工具类
 *
 * @author xinwei.sun
 *
 */
public class ServiceUtil {
	private static final double EARTH_RADIUS = 6378137.0;

	/***
	 *
	 * @param longitude1
	 *            目的坐标 经度
	 * @param latitude1
	 *            纬度
	 * @param longitude2
	 *            当前坐标 经度
	 * @param latitude2
	 *            纬度
	 * @return 两个坐标之间的距离 单位（米）
	 */
	public static double getDistance(double longitude1, double latitude1,
			double longitude2, double latitude2) throws Exception {
		double Lat1 = rad(latitude1);
		double Lat2 = rad(latitude2);
		double a = Lat1 - Lat2;
		double b = rad(longitude1) - rad(longitude2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(Lat1) * Math.cos(Lat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/***
	 *
	 * @param context
	 * @param lon
	 *            精度
	 * @param lat
	 *            纬度
	 * @return 是否进入围栏
	 */
	public static boolean getCachelocation(Context context, String lon,
			String lat) throws Exception {
		Boolean isIn = false;
		String lo = "";
		String la = "";
		SharedPreferences preferences = context.getSharedPreferences(
				"SmecMntMapService", Context.MODE_PRIVATE);
		lo = preferences.getString("lon", "0");
		la = preferences.getString("lat", "0");
		double distance = getDistance(Double.parseDouble(lo),
				Double.parseDouble(la), Double.parseDouble(lon),
				Double.parseDouble(lat));
		if (distance < 500) {// 设置围栏半径 单位（米）
			isIn = true;
		}
		return isIn;
//		return false;
	}
	/**
	 * 获取是否进入围栏
	 * @param context
	 * @param lon 电梯经度
	 * @param lat 电梯纬度
	 * @param lo  用户经度
	 * @param la  用户纬度
	 * @return
	 */
	public static boolean getIsInNow(Context context, String lon,
			String lat,String lo, String la){
		Boolean isIn = false;
		double distance = 0;
		try {
			distance = getDistance(Double.parseDouble(lo),
					Double.parseDouble(la), Double.parseDouble(lon),
					Double.parseDouble(lat));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (distance < 500) {// 设置围栏半径 单位（米）
			isIn = true;
		}
		return isIn;
	}

	/***
	 *
	 * @param context
	 * @param position
	 *            位上传坐标列表位置
	 * @return 未上传的坐标
	 */
	public static String getCachelocation(Context context, int position)
			throws Exception {
		String locationInfos[] = {};
		String locations[] = {};
		Fileservice fileservice = new Fileservice(context);
		String locationInfo = "log"
				+ fileservice.readSDFile("smecLocationlog.txt");
		locationInfos = locationInfo.split(",");
		String location = locationInfos[position + 1];
		locations = location.split("&");
		String value = "?lon=" + locations[0] + "&lat=" + locations[1]
				+ "&user=" + locations[2] + "&type=" + locations[3] + "&date="
				+ locations[4] + "&device=" + locations[5];
		return value;
	}

	/***
	 *
	 * @param context
	 * @return 返回未上传坐标条数
	 * @throws Exception
	 */
	public static int getCachelocation(Context context) throws Exception {
		String locationInfos[] = {};
		Fileservice fileservice = new Fileservice(context);
		String locationInfo = "log"
				+ fileservice.readSDFile("smecLocationlog.txt");
		locationInfos = locationInfo.split(",");
		return locationInfos.length - 1;
	}

	/**
	 * 强制帮用户打开GPS
	 *
	 * @param context
	 */
	public static void openGPS(Context context) {
		Intent GPSIntent = new Intent();
		GPSIntent.setClassName("com.android.settings",
				"com.android.settings.widget.SettingsAppWidgetProvider");
		GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
		GPSIntent.setData(Uri.parse("custom:3"));
		try {
			PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
		} catch (CanceledException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 判断GPS是否开启
	 *
	 * @param context
	 * @return true 表示开启
	 */
	public static final boolean isOPen(final Context context) {
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		boolean gps = locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (gps) {
			return true;
		}
		return false;
	}
}
